package com.ruoyi.web.Utils;

import com.alibaba.fastjson.JSON;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.springframework.core.annotation.AliasFor;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * @author wjz
 * @date 2021/5/17
 */
@Slf4j
public class SmsUtils {

    /**
     * 发送验证码模板短信
     * @param mobile
     * @param identifyCode
     * @return
     */
    public static boolean sendMessage(String mobile, String identifyCode) {
        boolean result = false;
        //发送验证码模板短信
        if (sendTemplatesSms(mobile, identifyCode,"183287")) {
            return true;
        }
        return result;
    }

    /**
     * 发送推广短信，非模板短信
     * @param mobile
     * @param content
     * @return
     */
    public static boolean sendDIYMessage(String mobile, String content){
        if(sendSms(mobile,content,false,null)){
            return true;
        }
        return false;
    }

    /**
     * 发送指定模板id的短信
     * @param mobile
     * @param templateArgs
     * @param templateId
     * @return
     */
    public static boolean sendTemplatesSms(String mobile,String templateArgs,String templateId){
        return sendSms(mobile,templateArgs,true,templateId);
    }

    private static boolean sendSms(String mobile, String identifyContent, boolean isTemplateSms, String templateId) {
        try {
            String requestUri = "http://api.feige.ee/SmsService/Send";
            CloseableHttpClient client = null;
            CloseableHttpResponse response = null;
            try {
                List<BasicNameValuePair> formparams = new ArrayList<>();
                formparams.add(new BasicNameValuePair("Account", "15760594106"));
                formparams.add(new BasicNameValuePair("Pwd", "1aecae90929c3376e82e3fd88"));
                formparams.add(new BasicNameValuePair("Content", identifyContent));
                formparams.add(new BasicNameValuePair("Mobile", mobile));
                formparams.add(new BasicNameValuePair("SignId", "335243"));
                if(isTemplateSms){
                    formparams.add(new BasicNameValuePair("TemplateId", templateId));
                    requestUri = "http://api.feige.ee/SmsService/Template";
                }
                HttpPost httpPost = new HttpPost(requestUri);
                httpPost.setEntity(new UrlEncodedFormEntity(formparams, "UTF-8"));
                client = HttpClients.createDefault();
                response = client.execute(httpPost);
                HttpEntity entity = response.getEntity();
                String responseResult = EntityUtils.toString(entity);
                log.info("短信验证码响应：{}", responseResult);
                Map<String, String> map = JSON.parseObject(responseResult, Map.class);
                if ("OK".equals(map.get("Message"))) {
                    return true;
                }
            } finally {
                if (response != null) {
                    response.close();
                }
                if (client != null) {
                    client.close();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("发送验证码异常，原因：{}", e.getMessage());
        }
        return false;
    }

//    public static void main(String[] args) {
////        final boolean b = sendMessage("15760594106", "七院教学部||教学新环境交流||线上||党小组会||http://ipbp.hhzzss.cn");
//        /**
//         * @部门，将于@在@开展@会议，会议类型为：@，会议主题为@，会议内容为@。会议资料请看：https://ipbp.hhzzss.cn:81/system/acti/file/@。请您准时参加，若无法到场，请于会前通过小程序进行请假申请。确认收到本条通知请点击:https://ipbp.hhzzss.cn:81/system/acti/confirmSmsNotification/@
//         */
//        boolean b = sendTemplatesSms("15760594106","七院||明天中午||飞天楼||党小组||党小组会议||谈点小组任务||新环境如何面对||29||uuid", SmsTemplateId.会议预启动资料下发模板.getInfo());
//        System.out.println(b);
//    }

    /**
     * 产生6位随机数(000000-999999)
     *
     * @return 6位随机数
     */
    public static String getSixRandom() {
        return new Random().nextInt(899999) + 100000 + "";
    }
}
